package com.aizhixin.lab.task.service;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.aizhixin.lab.common.core.DataValidity;


@Repository
public class TaskQuestionJDBC {

	@Autowired
	private JdbcTemplate jdbcTemplate;


	public void delOrderNum(String workLibId, Integer orderNum,String id) {
		String sql = "UPDATE t_task_question SET order_num = (order_num - 1)";
		sql += " WHERE task_id = '" + workLibId + "' AND order_num > '" + orderNum + "' and delete_flag = '"
				+ DataValidity.VALID.getState()+ "';";
		String sql2="UPDATE `t_task_question` AS e SET e.`delete_flag`=1 WHERE e.`id`='"+id+"';";
		
		jdbcTemplate.batchUpdate(sql,sql2);
	}
	
	/**
	 * 
	 * @Title: updateOrderNum 
	 * @Description: 新增试题后更新试题序号
	 * @param @param workLibId
	 * @param @param orderNum
	 * @return void 
	 * @throws
	 */
	public void updateOrderNum(String taskId, Integer orderNum) {
		String sql = "UPDATE t_task_question SET order_num = (order_num + 1)";
		sql += " WHERE task_id = '" + taskId + "' AND order_num > '" + orderNum + "' and delete_flag = '"
				+ DataValidity.VALID.getState()+ "'";
		jdbcTemplate.update(sql);
	}
	
	
	public Integer countByTaskIdAndDeleteFlag(String taskId,Integer deleteFlag) {
		String sql="SELECT COUNT(*) FROM `t_task_question` AS ewq WHERE ewq.`task_id`='"+taskId+"' AND ewq.`delete_flag`="+deleteFlag;
		return jdbcTemplate.queryForObject(sql, Integer.class);
		
	}
}
